package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uwlaser.domain.*;
import com.uwlaser.service.FactoryShiftService;
import com.uwlaser.service.RequestParamService;
import com.uwlaser.service.dto.FactoryShiftDto;
import com.uwlaser.service.mapper.CraftOperationMapper;
import com.uwlaser.service.mapper.DeviceMainMapper;
import com.uwlaser.service.mapper.FactoryModelMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.enums.DictValueEnum;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RequestParamServiceImpl implements RequestParamService {

    //设备列表
    private final DeviceMainMapper deviceMainMapper;

    //工厂模型
    private final FactoryModelMapper factoryModelMapper;

    //工序信息
    private final CraftOperationMapper craftOperationMapper;
    //班次信息
    private final FactoryShiftService factoryShiftService;

    @Override
    public RequestParam  getRequestParam(Object param) {

        RequestParam requestParam = new RequestParam<>();

        requestParam.setParam(param);

        if(param instanceof BaseParam){

           BaseParam baseParam = (BaseParam) param;
           //得到设备编码
           DeviceMain deviceMain = getDeviceMain(baseParam.getDeviceCode());
            //查询设备信息
           requestParam.setDeviceMain(deviceMain);

           //获取工位模型信息
           requestParam.setFactoryStation(getFactoryModel(deviceMain.getStationCode(),DictValueEnum.MODEL_TYPE_STATION));

           //获取工段模型信息
           requestParam.setFactorySection(getFactoryModel(deviceMain.getSectionCode(),DictValueEnum.MODEL_TYPE_SECTION));

           //获取产线模型信息
           requestParam.setFactoryLine(getFactoryModel(deviceMain.getLineCode(),DictValueEnum.MODEL_TYPE_LINE));

           //获取当前工序信息
           requestParam.setCraftOperation(getCraftOperation(baseParam.getDeviceCode()));

           //操作人信息
           requestParam.setOperator(baseParam.getOperator());

           //班次信息
           requestParam.setFactoryShift(getFactoryShift(requestParam.getFactoryLine().getModelCode()));

        }

        return requestParam;
    }

    /**
     * 获取设备信息
     * @param deviceCode
     * @return
     */
    public DeviceMain getDeviceMain(String deviceCode){
        DeviceMain deviceMain = new DeviceMain();
        deviceMain.setDeviceCode(deviceCode);
        //查询设备信息
        DeviceMain resDevice = deviceMainMapper.selectOne(new QueryWrapper<>(deviceMain));
        if(resDevice == null){
            throw new RuntimeException("设备编码【"+deviceCode+"】错误！MES系统没有维护该设备信息，请维护后再试！");
        }
        return resDevice;
    }

    /**
     * 获取工厂模型信息
     * @return
     */
    public FactoryModel getFactoryModel(String code,DictValueEnum type){
        FactoryModel factoryModel = new FactoryModel();
        factoryModel.setModelCode(code);
        factoryModel.setModelType(type.getValue());
        FactoryModel resModel = factoryModelMapper.selectOne(new QueryWrapper<>(factoryModel));
        if(resModel == null){
            throw new RuntimeException("通过" + type.getLabel() + "编号【"+code+"】获取模型信息失败！MES系统没有维护该模型信息，请维护后再试！");
        }
        return resModel;
    }

    /**
     * 获取工序信息
     * @param deviceCode
     * @return
     */
    public CraftOperation getCraftOperation(String deviceCode){
        CraftOperation craftOperation = craftOperationMapper.selectOneByDeviceCode(deviceCode);
        if(craftOperation == null){
            throw new RuntimeException("通过设备编号【"+deviceCode+"】获取工序信息失败！该设备没有绑定到任何设备下！");
        }
        return craftOperation;
    }


    @Override
    public DataRequestParam getDataRequestParam(Object param) {
        DataRequestParam requestParam = new DataRequestParam<>();

        requestParam.setParam(param);

        if(param instanceof BaseParam){

            BaseParam baseParam = (BaseParam) param;
            //得到设备编码
            DeviceMain deviceMain = getDeviceMain(baseParam.getDeviceCode());
            //查询设备信息
            requestParam.setDeviceMain(deviceMain);

            //获取工位模型信息
            requestParam.setFactoryStation(getFactoryModel(deviceMain.getStationCode(),DictValueEnum.MODEL_TYPE_STATION));

            //获取工段模型信息
            requestParam.setFactorySection(getFactoryModel(deviceMain.getSectionCode(),DictValueEnum.MODEL_TYPE_SECTION));

            //获取产线模型信息
            requestParam.setFactoryLine(getFactoryModel(deviceMain.getLineCode(),DictValueEnum.MODEL_TYPE_LINE));

            //获取当前工序信息
            requestParam.setCraftOperation(getCraftOperation(baseParam.getDeviceCode()));

            //操作人信息
            requestParam.setOperator(baseParam.getOperator());

        }

        return requestParam;
    }

    /**
     * 获取班次信息
     */
    public FactoryShift getFactoryShift(String modelCode) {
        FactoryShiftDto factoryShiftDto = new FactoryShiftDto();
        FactoryShiftDto factoryShiftDto1 = factoryShiftDto.setTemplateCode(modelCode);
        FactoryShiftDto classes = factoryShiftService.getClasses(factoryShiftDto1);
        return ConvertUtil.convert(classes, FactoryShift.class);

    }
}

